package DAO;

import java.util.ArrayList;

import DTO.picturesDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

public class picturesDAO {
	public static final String KEY_MAGHICHU = "maghichu";
    public static final String KEY_URL = "urlPic";
    public static final String KEY_ROWID = "_id";
    
    private static DatabaseHelper mDbHelper;
    private static  SQLiteDatabase mDb;
    public static final String TABLE_PICTURES = "pictures";
    

    public picturesDAO() {
    }

    public static void open(Context ctx) throws SQLException {
        mDbHelper = new DatabaseHelper(ctx);
        mDb = mDbHelper.getWritableDatabase();
    }

    public static void close() {
        mDbHelper.close();
    }
    
    public static long createPictures(int mapic,long maghichu, String url) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_ROWID, mapic);
        initialValues.put(KEY_MAGHICHU, maghichu);
        initialValues.put(KEY_URL, url);
        
        return mDb.insert(TABLE_PICTURES, null, initialValues);
    }
    public static boolean deletePicture(long rowId) {
        return mDb.delete(TABLE_PICTURES, KEY_ROWID + "=" + rowId, null) > 0;
    }
    
    public static boolean XoaTatCaHinhCuaMotGhiChu(long rowId) {
        return mDb.delete(TABLE_PICTURES, KEY_MAGHICHU + "=" + rowId, null) > 0;
    }
    public static String getUrl(long rowId)
    {
    	Cursor c = mDb.query(TABLE_PICTURES, new String[] {KEY_URL},KEY_ROWID +"="+rowId, null, null, null, null,null);
    	//int idcolum = c.getColumnIndex(KEY_URL);
    	if(c != null)
    	{
    		if(c.moveToFirst())
    		{
    			return "2";
    		}
    		//String url=c.getString(idcolum);
    		return "3";
    	}
    	else
    		return "h";
    }
    public static ArrayList<picturesDTO> getAllPic() {

    	Cursor cursor = mDb.query(TABLE_PICTURES, new String[] {KEY_ROWID, KEY_MAGHICHU, KEY_URL}, 
    			null, null, null, null, null);
    	ArrayList<picturesDTO> picArr = new ArrayList<picturesDTO>();
        
        
        if (cursor != null ) {
            if  (cursor.moveToFirst()) {
                do {
                	picturesDTO temp = new picturesDTO();
                    temp.setUrlPic(cursor.getString(cursor.getColumnIndex(picturesDAO.KEY_URL)));
                    temp.setMaghichu(cursor.getLong(cursor.getColumnIndex(picturesDAO.KEY_MAGHICHU)));
                    temp.setMaPic(cursor.getInt(cursor.getColumnIndex(picturesDAO.KEY_ROWID)));
                    picArr.add(temp);
                }while (cursor.moveToNext());
            }
        }
        
        return picArr;
    }
    
    public static ArrayList<picturesDTO> getAllPicfromNote(long rowId) throws SQLException {
    	
        Cursor cursor = mDb.query(TABLE_PICTURES, new String[] {KEY_ROWID, 
    			KEY_MAGHICHU, KEY_URL},KEY_MAGHICHU + "=" + rowId
    			,null, null, null, null, null);
        ArrayList<picturesDTO> picArr = new ArrayList<picturesDTO>();
        
        
        if (cursor != null ) {
            if  (cursor.moveToFirst()){
                do {
                	picturesDTO temp = new picturesDTO();
                    temp.setUrlPic(cursor.getString(cursor.getColumnIndex(picturesDAO.KEY_URL)));
                    temp.setMaghichu(cursor.getLong(cursor.getColumnIndex(picturesDAO.KEY_MAGHICHU)));
                    temp.setMaPic(cursor.getInt(cursor.getColumnIndex(picturesDAO.KEY_ROWID)));
                    picArr.add(temp);
                }while (cursor.moveToNext());
            }
        }
        return picArr;
    }
    public static picturesDTO getPic(long rowId) throws SQLException {

        Cursor mCursor =
            mDb.query(true, TABLE_PICTURES, new String[] {KEY_ROWID,
            		KEY_MAGHICHU,KEY_URL}, KEY_ROWID + "=" + rowId, null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        picturesDTO temp = new picturesDTO();
        temp.setUrlPic(mCursor.getString(mCursor.getColumnIndex(picturesDAO.KEY_URL)));
        temp.setMaghichu(mCursor.getLong(mCursor.getColumnIndex(picturesDAO.KEY_MAGHICHU)));
        temp.setMaPic(mCursor.getInt(mCursor.getColumnIndex(picturesDAO.KEY_ROWID)));
        
        return temp;

    }
    public static boolean updatePic(long rowId, long maghichu, String url) {
        ContentValues args = new ContentValues();
        args.put(KEY_MAGHICHU, maghichu);
        args.put(KEY_URL, url);

        return mDb.update(TABLE_PICTURES, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}
